﻿
Public Class DialogBayarAll
    Public returnbayar As Boolean
    
    Private Sub setcombo()
        Dim dtbank As New DataTable
        Dim dtkartu As New DataTable
        If Utils.executeSP("proc_master_bank", New Object() {"select", Convert.DBNull}, dtbank) Then
            If dtbank IsNot Nothing Then
                If dtbank.Rows.Count > 0 Then
                    ComBank.DataSource = dtbank
                    ComBank.DisplayMember = "nama"
                    ComBank.ValueMember = "nama"
                End If
            End If
        End If
        If Utils.executeSP("proc_master_kartu", New Object() {"select", Convert.DBNull}, dtkartu) Then
            If dtkartu IsNot Nothing Then
                If dtkartu.Rows.Count > 0 Then
                    Comkartu.DataSource = dtkartu
                    Comkartu.DisplayMember = "nama"
                    Comkartu.ValueMember = "nama"
                End If
            End If
        End If
    End Sub

    Private Sub aktifbank(ByVal status As Boolean)
        If status Then
            ComBank.Enabled = True
            Comkartu.Enabled = True
            txttrx.Enabled = True

            'txtnumbank.Enabled = True
            'txtnumbank.ReadOnly = False
            Dim datat As Double = IIf(Not txtTotal.NumericText = String.Empty, CDbl(txtTotal.NumericText), 0)
            txtnumbank.Text = datat.ToString
            totalbayar()
            txtnumcash.Enabled = False
            setcombo()
            ComBank.Focus()
        Else
            ComBank.Enabled = False
            Comkartu.Enabled = False
            txttrx.Enabled = False
            txtnumbank.Enabled = False
            txtnumcash.Enabled = True
            txtnumcash.Focus()
        End If


    End Sub
    Private Sub totalbayar()
        Dim vbank, vcash, byar As Double
        If txtnumbank.NumericText = String.Empty Then
            vbank = 0
        Else
            vbank = txtnumbank.NumericText
        End If

        If txtnumcash.NumericText = String.Empty Then
            vcash = 0
        Else
            vcash = txtnumcash.NumericText
        End If


        byar = vbank + vcash
        txtBayar.Text = byar

    End Sub

    Private Sub cbxBayar_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxBayar.CheckedChanged
        aktifbank(cbxBayar.Checked)
    End Sub

    Private Sub txtnumbank_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtnumbank.TextChanged
        'If ComBank.SelectedValue.ToString = "" Then
        '    txtnumbank.Text = 0
        'ElseIf Comkartu.SelectedValue.ToString = "" Then
        '    txtnumbank.Text = 0
        'Else
        'If txttrx.Text = "" Then
        '    txtnumbank.Text = 0
        'End If
    End Sub

    Private Sub txtnumbank_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtnumbank.KeyDown
        'If e.KeyValue = 13 Then
        '    txtnumcash.Focus()
        'End If
        
    End Sub
    Public parentme As String
    Private Sub txtnumcash_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtnumcash.KeyDown
        If e.KeyValue = 13 Then
            Dim total As Double = 0
            Dim kembali As Double = 0
            Dim bayar As Double = 0

            Try

                total = CDbl(txtTotal.NumericText)
                bayar = CDbl(txtBayar.NumericText)
                kembali = bayar - total

                If total < bayar Or total = bayar Then
                    returnbayar = True
                    'If parentme.ToLower = "pembayaranpage" Then
                    '    returnbayar = True
                    'ElseIf parentme.ToLower = "penjualanobatpage" Then
                    '    returnbayar = True
                    'ElseIf parentme.ToLower = "penjualanobatumumpage" Then
                    '    returnbayar = True
                    'End If
                    Me.Close()
                ElseIf total > bayar Then
                    If parentme.ToLower = "trxpembelianpage" Then
                        returnbayar = True
                        Me.Close()
                        Exit Sub
                    End If
                    If MessageBox.Show("Maaf Jumlah Bayar masih Kurang" & vbCrLf & _
                                       "Apakah akan mengulangi bayar ? ", "Informasi Bayar", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                        If txtnumbank.Enabled Then
                            txtnumbank.Focus()
                        Else
                            txtnumcash.Focus()
                        End If
                        'txtKembali.Text = total
                        'txtBayar.Focus()
                    Else
                        Me.Close()
                    End If

                End If
            Catch ex As Exception
                txtBayar.Text = 0
            End Try
        End If
    End Sub

    Private Sub txtnumcash_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtnumcash.TextChanged
        totalbayar()
    End Sub

    Private Sub txtnumbank_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtnumbank.KeyUp
        totalbayar()
    End Sub

    Private Sub txtnumcash_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtnumcash.KeyUp
        totalbayar()
    End Sub

    Private Sub DialogBayarAll_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        returnbayar = False
    End Sub

    Private Sub txttrx_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txttrx.Leave
        If txttrx.Text = "" Then
            MsgBox("Transksi Code Tidak Valid")
            cbxBayar.Checked = False

        End If

    End Sub
   
    Private Sub DialogBayarAll_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown

    End Sub

    Private Sub FocusCashToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FocusCashToolStripMenuItem.Click
        txtnumcash.Focus()
    End Sub

    Private Sub txtBayar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBayar.TextChanged
        Dim total As Double = 0
        Dim kembali As Double = 0
        Dim bayar As Double = 0
        If txtBayar.Text = "" Then
            txtBayar.Double = 0
        End If
        Try

            total = CDbl(txtTotal.NumericText)
            bayar = CDbl(txtBayar.NumericText)
            kembali = bayar - total
            txtKembali.Double = kembali

            'If total < bayar Or total = bayar Then
            '    txtKembali.Text = kembali
            'ElseIf total > bayar Then
            '    txtKembali.Text = total
            'End If
        Catch ex As Exception
            txtBayar.Double = 0
        End Try
    End Sub

    Private Sub txttrx_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txttrx.KeyDown
        If e.KeyValue = 13 Then
            Dim total As Double = 0
            Dim kembali As Double = 0
            Dim bayar As Double = 0

            If cbxBayar.Checked Then
                If txttrx.Text = String.Empty Then
                    MsgBox("Maaf Data Belum Lengkap")
                    txttrx.Focus()
                    Exit sub
                End If
            End If

            Try

                total = CDbl(txtTotal.NumericText)
                bayar = CDbl(txtBayar.NumericText)
                kembali = bayar - total

                If total < bayar Or total = bayar Then
                    returnbayar = True
                    'If parentme.ToLower = "pembayaranpage" Then
                    '    returnbayar = True
                    'ElseIf parentme.ToLower = "penjualanobatpage" Then
                    '    returnbayar = True
                    'ElseIf parentme.ToLower = "penjualanobatumumpage" Then
                    '    returnbayar = True
                    'End If
                    Me.Close()
                ElseIf total > bayar Then
                    If parentme.ToLower = "trxpembelianpage" Then
                        Me.Close()
                    End If
                    If MessageBox.Show("Maaf Jumlah Bayar masih Kurang" & vbCrLf & _
                                       "Apakah akan mengulangi bayar ? ", "Informasi Bayar", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                        If cbxBayar.Checked Then
                            txttrx.Focus()
                        Else
                            txtnumcash.Focus()
                        End If
                        'txtKembali.Text = total
                        'txtBayar.Focus()
                    Else
                        Me.Close()
                    End If

                End If
            Catch ex As Exception
                txtBayar.Text = 0
            End Try
        End If
    End Sub
End Class